package com.service.admin.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.admin.model.SysUserPage;
import com.service.admin.base.controller.req.SysFILConfigPageReq;
import com.service.admin.base.controller.req.SysFILQutoPerRewardReq;
import com.service.admin.base.service.SysFilConfigService;
import com.service.admin.support.MemCache;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.mapper.SysFilConfigMapper;
import com.service.dao.mapper.SysQuotesIpfxMapper;
import com.service.dao.model.SysFilConfig;
import com.service.dao.model.SysQuotesIpfx;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
@Slf4j
public class SysFilConfigServiceImpl extends ServiceImpl<SysFilConfigMapper, SysFilConfig> implements SysFilConfigService {

	@Autowired
	private SysFilConfigMapper filPoolMapper;

	@Autowired
	private SysQuotesIpfxMapper sysQuotesIpfxMapper;

	public final static String REDIS_IPFS_KEY = "ipfs_config";

	@Autowired
	private MemCache memCache;

	/**
	 * 分页查询FILPOOL配置
	 * @param req
	 * @return
	 */
	@Override
	public CommonPageRep<SysFilConfig> pageFILConfig(SysFILConfigPageReq req) {
		Page<SysUserPage> page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
		LambdaQueryWrapper<SysFilConfig> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(SysFilConfig::getCreateTime);
		List<SysFilConfig> list = filPoolMapper.selectList(queryWrapper);
		return CommonPageRep.buildResultRep(page,list);
	}


	@Override
	public int modifyFilQutoPerReward(SysFILQutoPerRewardReq sysFILQutoPerRewardReq){
		if(StringUtils.isNotEmpty(sysFILQutoPerRewardReq.getPerReward())){
			LambdaQueryWrapper<SysQuotesIpfx> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.orderByDesc(SysQuotesIpfx::getUpdateTime);
			queryWrapper.last("limit 1");
			SysQuotesIpfx ipfxConfig = sysQuotesIpfxMapper.selectOne(queryWrapper);
			String per_24h_rewards = sysFILQutoPerRewardReq.getPerReward()+" FIL/TiB";
			ipfxConfig.setPer_24h_rewards(per_24h_rewards);
			String json = JSONObject.toJSON(ipfxConfig).toString();
			//添加/更新到redis缓存
			memCache.setNoExpire(REDIS_IPFS_KEY,json);
			return sysQuotesIpfxMapper.updateById(ipfxConfig);
		}
		return 0;
	}

	@Override
	public String initPerReward() {
		LambdaQueryWrapper<SysQuotesIpfx> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(SysQuotesIpfx::getUpdateTime);
		queryWrapper.last("limit 1");
		SysQuotesIpfx ipfxConfig = sysQuotesIpfxMapper.selectOne(queryWrapper);
		return ObjectUtils.isEmpty(ipfxConfig) ? "0" : ipfxConfig.getPer_24h_rewards();
	}
}
